package zhongbo.javaweb.instagram.util;

import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.Map;

/**
 * 说明：
 *
 * @author zhongboWork
 * @version 1.0
 * @since 2021-12-30 19:10
 */
@Slf4j
public class StrUtil {

    /**
     * 截取指定长度的字符串
     * (有点玄的截取长度)
     *
     * @param str    原始字符串
     * @param start  开始位置(偏移)
     * @param length 截取长度(偏移)
     * @return 截取后的字符串
     */
    public static String subStr(String str, int start, int length) {
        String result = "error";
        if (null != str) {
            int strLen = str.length();
            if (start < 0) {
                start %= strLen;
                start += strLen;
            }
            if (length < 0) {
                start += length;
                length = -length;
                if (start < 0) {
                    length += start;
                    start = 0;
                }
            }
            int end = start + length;
            if (start > strLen) {
                end = strLen;
                start = end;
            }
            if (end > strLen) {
                end = strLen;
            }
            try {
                result = str.substring(start, end);
            } catch (Exception e) {
                log.error("哎呦! 走错地方了! {},{},{} ignore: ", str, start, length, e);
            }
        }
        return result;
    }
}
